home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / langs / xlisp2.1 / xldist01.zoo / lsp / akalah.lsp < prev    next >
Encoding:
Lisp/Scheme  |  1990-11-09  |  8.8 KB  |  297 lines

  1. ; To play against the computer:
  2. ; (meplay #pits-per-side #pebbles-per-pit #search-depth #computer-plays-first)
  3. ;
  4. ; To have the computer play against itself:
  5. ; (play #pits-per-side #pebbles-per-pit #search-depth)
  6.  
  7.  
  8. ; The playing arena:
  9. ; 13    12 11 10 9  8  7
  10. ;    0  1  2  3  4  5     6 
  11.  
  12.  
  13. (defun makelist (length contents)
  14.     (if    (zerop length)
  15.         'nil
  16.         (cons contents (makelist (1- length) contents))))
  17.  
  18. (setq *maxvalue* 1000)
  19. (setq *minvalue* (- *maxvalue*))
  20. (defun copy (list) (append list 'nil))
  21. (defun prinb (x) (dotimes (i x) (princ #\space)))
  22.  
  23. (defun search (startpos depth whoseturn)
  24.    (if (zerop depth)
  25.        (list startpos (evaluate startpos whoseturn))
  26.        (let (bestval nextval bestpos succlist)
  27.         (setq succlist (successorsfn startpos whoseturn))
  28.         (when (> depth 1) (setq succlist (reorder succlist whoseturn)))
  29.         (setq 
  30.             beta    *maxvalue*
  31.             bestval *minvalue*
  32.         bestpos (car succlist))
  33.         (dolist (this succlist)
  34.             (when (wincheck this whoseturn)
  35.             (return-from search (list this *maxvalue*)))
  36.         (setq nextval (- (alphabeta this
  37.                         (- beta)
  38.                         (- bestval)
  39.                         (1- depth)
  40.                         (not whoseturn))))
  41.         (when (> nextval bestval)
  42.               (setq bestval nextval)
  43.               (setq bestpos this)))
  44.         (list bestpos bestval))))    ; return value
  45.  
  46. (defun alphabeta (position alpha beta depth whoseturn)
  47.     (if (zerop depth)
  48.         (evaluate position whoseturn)
  49.     (let (bestval nextval succlist)
  50.         (setq succlist (successorsfn position whoseturn))
  51.         (when (> depth 1) (setq succlist (reorder succlist whoseturn)))
  52.         (setq bestval alpha)
  53.         (dolist (this succlist)
  54.             (when (wincheck this whoseturn)
  55.               (return-from alphabeta *maxvalue*))
  56.         (setq nextval (- (alphabeta this
  57.                         (- beta)
  58.                         (- bestval)
  59.                         (1- depth)
  60.                         (not whoseturn))))
  61.         (when (> nextval bestval) (setq bestval nextval))
  62.         (when (<= beta bestval) (return-from alphabeta bestval)))
  63.         bestval)))
  64.  
  65. (defun successorsfn (position whoseturn 
  66.             &aux 
  67.             (picuphole (1- (if whoseturn *firstb* *firsta*)))
  68.             succlist 
  69.             succ 
  70.             stones 
  71.             disthole 
  72.             lasthole)
  73.    (dotimes (dummy *enda*)
  74.         (when (not (zerop (nth (setq picuphole (1+ picuphole)) position)))
  75.           (setq    succ (copy position))
  76.           (setq stones (nth picuphole succ)) ; stones in this pit
  77.           (empty succ picuphole)
  78.           (setq disthole picuphole)
  79.           (dotimes (dummy2 stones) ; drop in successive holes except
  80.                          ; opponents kalah hole
  81.                  (setq disthole (nextdistholefn disthole whoseturn))
  82.                (dropin succ disthole 1))
  83.           (setq lasthole disthole)
  84.           (cond ((allownzerok succ whoseturn) ; all played out
  85.                (opptakesallfn succ whoseturn))
  86.             ((eq lasthole (kalaholefn whoseturn)) ; last in kalah
  87.              (setq succ (successorsfn succ whoseturn)))
  88.             ((and (eq (nth lasthole succ) 1) ; last into own empty
  89.                   (> (nth (opposholefn lasthole) succ) 0)
  90.                   (ownsidep lasthole whoseturn))
  91.              (dropin succ 
  92.                   (kalaholefn whoseturn)
  93.                  (1+ (nth (opposholefn lasthole) succ)))
  94.              (empty succ lasthole)
  95.              (empty succ (opposholefn lasthole))
  96.              (when (allownzerok succ whoseturn)
  97.                    (opptakesallfn succ whoseturn))))
  98.           (setq succlist (nconc (preparelisfn succ) succlist))))
  99.     (if (null succlist)
  100.         (progn    (setq succ (copy position))
  101.         (opptakesallfn succ whoseturn)
  102.         (list succ))
  103.     succlist))
  104.  
  105. (defun dropin (position hole number) 
  106.     (rplaca (nthcdr hole position)
  107.         (+ number
  108.            (nth hole position))))
  109.  
  110. (defun nextdistholefn (disthole whoseturn)
  111.     (cond    ((and whoseturn (eql disthole *lasta*)) *firstb*) ; skip own pile
  112.         ((and (not whoseturn) (eql disthole *lastb*)) *firsta*)
  113.         ((< disthole *endb*) (1+ disthole))
  114.         (t *firsta*)))
  115.  
  116. (defun empty (position hole)    ; empty out the given hole
  117.     (rplaca (nthcdr hole position) 0))
  118.  
  119.  
  120. (defun kalaholefn (whoseturn)   ; the scoring hole for the given player
  121.     (if whoseturn *endb* *enda*))
  122.  
  123. (defun opposholefn (hole)    ; calculate the opposing hole
  124.      (- *lastb* hole))
  125.  
  126. (defun ownsidep (hole whoseturn)
  127.     (if whoseturn (> hole *enda*) (< hole *firstb*)))
  128.  
  129. (defun preparelisfn (x)
  130.     (if (atom (car x))
  131.         (list x)
  132.         (unimbedfn x)))
  133.  
  134. (defun reorder (poslist whoseturn)
  135.     (sortdown poslist (statvalsfn poslist whoseturn)))
  136.  
  137.  
  138. (defun sortdown (poslist statvalvec)
  139.     (if (null (cdr poslist))
  140.         poslist
  141.         (let ((maxindex (indexfn statvalvec)))
  142.              (cons (nth maxindex poslist)
  143.                (sortdown (deletel maxindex poslist)
  144.                         (deletel maxindex statvalvec))))))
  145.  
  146. (defun deletel (index list)    ; delete the index'th value in list
  147.     (if (zerop index)
  148.         (cdr list)
  149.         (prog2
  150.           (rplacd (nthcdr (1- index) list) (nthcdr (1+ index) list))
  151.           list)))
  152.  
  153. (defun indexfn (x) 
  154.        (let ((v (apply #'max x)))
  155.         (position-if #'(lambda (q) (eql q v)) x)))
  156.        
  157. (defun statvalsfn (poslist whoseturn)
  158.     (if (null poslist)
  159.         'nil
  160.         (cons (evaluate (car poslist) whoseturn)
  161.               (statvalsfn (cdr poslist) whoseturn))))
  162.  
  163. (defun wincheck (position whoseturn)
  164.     (> (nth (kalaholefn whoseturn) position) *halfall*))
  165.  
  166. (defun evaluate (position whoseturn) ; assign the value to the position
  167.                      ; could obviously use work
  168.     (let ((ownkala (nth (kalaholefn whoseturn) position))
  169.           (oppkala (nth (kalaholefn (not whoseturn)) position)))
  170.          (cond ((> ownkala *halfall*) *maxvalue*)
  171.                 ((> oppkala *halfall*) *minvalue*)
  172.            (t (- ownkala oppkala)))))
  173.  
  174. (defun printpos (position)
  175.     (let ((minprint (reverse (subseq position 0 *firstb*)))
  176.           (maxprint (nthcdr *firstb* position)))
  177.          (terpri)
  178.          (prin1 (car minprint))
  179.          (prinb (if (> 10 (car minprint)) 3 2))
  180.          (setq minprint (cdr minprint))
  181.          (do ((list minprint (cdr list)))
  182.              ((null list))
  183.          (prin1 (car list))
  184.          (prinb (if (> 10 (car list)) 2 1)))
  185.          (terpri)
  186.          (prinb 4)
  187.          (do ((list maxprint (cdr list)))
  188.              ((null (cdr list)))
  189.          (prin1 (car list))
  190.          (prinb (if (> 10 (car list)) 2 1)))
  191.          (prinb 2)
  192.          (prin1 (car (last maxprint)))
  193.          (terpri)
  194.          (terpri)))
  195.  
  196. (defun allownzerok (position whoseturn)
  197.     (zerop (apply #'+ (if whoseturn 
  198.                   (subseq position *firstb* *endb*)
  199.                   (subseq position 0 *enda*)))))
  200.  
  201.  
  202. (defun opptakesallfn (position whoseturn)
  203.     (dropin position
  204.         (kalaholefn (not whoseturn))
  205.         (apply #'+ (if whoseturn 
  206.                    (subseq position 0 *enda*)
  207.                    (subseq position *firstb* *endb*))))
  208.     (do    ((count *enda* (1- count))
  209.          (hole (if whoseturn *firstb* *firsta*) (1+ hole)))
  210.         ((zerop count))
  211.         (empty position hole)))
  212.  
  213. (defun nextmove (depth whoseturn)
  214.     (terpri)
  215.     (setq *board* (search (car *board*) depth whoseturn))
  216.     (printpos (car *board*))
  217.     (print (cdr *board*))
  218.     (terpri))
  219.  
  220. ; (defun unimbedfn (poslist)
  221. ;    (cond    ((null poslist) 'nil)
  222. ;        ((atom (caar poslist))
  223. ;         (cons (car poslist) (unimbedfn (cdr poslist))))
  224. ;        (t (append (unimbedfn (car poslist))
  225. ;               (unimbedfn (cdr poslist))))))
  226.  
  227. (defun unimbedfn (poslist)
  228.     (do    ((list poslist (cdr list))
  229.          (result nil (if (atom (caar list))
  230.                   (cons (car list) result)
  231.                  (append (unimbedfn (car list)) result))))
  232.             ((null list) result)))
  233.  
  234. (defun initialize (holes pebbles &aux temp) ; initialize the playing area
  235.     (setq *enda* holes)
  236.     (setq *endb* (+ holes holes 1))
  237.     (setq *firsta* 0)
  238.     (setq *lasta* (1- *enda* ))
  239.     (setq *firstb* (1+ *enda*))
  240.     (setq *lastb* (1- *endb*))
  241.     (setq *halfall* (* holes pebbles))
  242.     (setq temp (makelist holes pebbles))
  243.     (setq *board* (list (append temp
  244.                     (list 0)
  245.                     temp
  246.                     (list 0))
  247.                 0)))
  248.  
  249. (defun play (holes pebbles depth) ; play the game
  250.     (initialize holes pebbles)
  251.     (do    ((whoseturn nil (not whoseturn)))
  252.         ((or (eql (cadr *board*) *maxvalue*)
  253.              (eql (cadr *board*) *minvalue*)))
  254.         (nextmove depth whoseturn)))
  255.  
  256. (defun meplay (holes pebbles depth computer-first) 
  257.   (prog (picuphole)
  258.     (initialize holes pebbles)
  259.     (when computer-first (setq succ (copy (car *board*)))
  260.           (go y))
  261. n    (setq succ (copy (car *board*)))
  262.     (printpos succ)
  263.     (if (> (nth *enda* succ) *halfall*) (return t))    ; win for side a
  264.     (if (> (nth *endb* succ) *halfall*) (return nil)) ; win for side b
  265. x    (princ "Hole? ") (setq picuphole (read))
  266.     (if (or (not (numberp picuphole))
  267.         (> picuphole *firstb*)
  268.         (> 1 picuphole)
  269.         (zerop (setq stones (nth (setq picuphole (1- picuphole)) succ))))
  270.         (go x))
  271.     (empty succ picuphole)
  272.     (setq disthole picuphole)
  273.     (dotimes (dummy stones)
  274.              (dropin succ (setq disthole (nextdistholefn disthole nil)) 1))
  275.     (setq lasthole disthole)
  276.     (cond    ((allownzerok succ nil)
  277.          (opptakesallfn succ nil)
  278.          (setq *board* (list succ 0))
  279.          (go n))
  280.         ((eql lasthole *enda*)
  281.          (setq *board* (list succ 0))
  282.          (go n))
  283.         ((and (eql (nth lasthole succ) 1)
  284.               (> (nth (opposholefn lasthole) succ) 0)
  285.               (> *enda* lasthole))
  286.          (dropin succ *enda* (1+ (nth (opposholefn lasthole) succ)))
  287.          (empty succ lasthole)
  288.          (empty succ (opposholefn lasthole))
  289.          (when (allownzerok succ nil)
  290.                (opptakesallfn succ nil)
  291.                (setq *board* (list succ 0))
  292.                (go n))))
  293.     (printpos succ)
  294. y    (setq *board* (search succ depth t))
  295.     (go n)))
  296.  
  297.